/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.ui.controls;

import ac.settings.dataModel;
import ac.ui.utils;
import java.awt.Graphics;
import java.awt.Graphics;
import javax.swing.JTextField;

/**
 *
 * @author empcl_000
 */
public class PCLCode extends JTextField implements dataModel {

    private boolean required;
    private boolean chiked;
    private boolean auto;

    public boolean isRequired() {
        return required;
    }

    public void setRequired(boolean required) {
        this.required = required;
    }

    public boolean isChiked() {
        return chiked;
    }

    public void setChiked(boolean required) {
        this.required = required;
    }

    public boolean isAuto() {
        return auto;
    }

    public void setAuto(boolean auto) {
        this.auto = auto;
    }

    private String table;

    public String getTable() {
        return table;
    }

    public void setTable(String table) {
        this.table = table;
    }

    public PCLCode() {
        this.setChiked(true);
        addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                CodeFocusLost(evt);
            }
        });
    }
    ac.lib.database.operations db = new ac.lib.database.operations();
    ac.uitls.propertiesOperation op = new ac.uitls.propertiesOperation();

    private void CodeFocusLost(java.awt.event.FocusEvent evt) {
        if (isChiked()) {
            String table = getTable();
            if (table != null) {
                String t = getText();
                String[] ise = db.getRow(table, "`" + FIELD_CODE + "`='" + t + "'");
                if (ise[0] != null) {
                    requestFocus();
                    utils.sendError(this, "", op.getWord("message.codeeError"));
                }
            }
        }

    }

    public void setCode() {
        String table = getTable();
        if (table != null) {
            if (isAuto()) {
                String[] ise = db.getRow(table, "max(id) as m", "");

                if (ise[0] != null) {
                    int code = Integer.parseInt(ise[0]) + 1;
                    setText("" + code);
                } else {
                    setText("1");
                }
            } else {
                setText("1");
            }
        }
    }

}
